package com.stnet.dao.enterprice.base;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Projections;

import com.modules.commons.Pager;
import com.stnet.bases.BaseDaoImpl;
import com.stnet.entity.Standby;

public class StandbyDaoImpl extends BaseDaoImpl implements StandbyDao {

	public Pager getStandbyList(int pageSize, int pageNo, String state) {
		Session session = getHibernateTemplate().getSessionFactory().openSession();
		Criteria criteria = session.createCriteria(Standby.class);
		int rowCount = ((Integer) criteria
				.setProjection(Projections.rowCount()).uniqueResult())
				.intValue();
		criteria.setProjection(null);
		int startIndex = pageSize * (pageNo - 1);
		criteria.setFirstResult(startIndex);
		criteria.setMaxResults(pageSize);
		List result = criteria.list();
		session.close();
		return new Pager(pageSize, pageNo, rowCount, result);
	}

	public void addStandby(Standby standby) {
		getHibernateTemplate().save(standby);
		
	}

	public void update(Standby standby) {
		getHibernateTemplate().update(standby);
		
	}

	public Standby getStandbyById(Long id) {
		
		return (Standby)getHibernateTemplate().get(Standby.class, id);
	}

	public void delete(Long id) {
		getHibernateTemplate().delete(getStandbyById(id));
		
	}
	
	public Standby getFirstEntity(){
		
		Standby standby = null;
		
		List list = this.getHibernateTemplate().find("from Standby");
		if(list.size()>0)
			standby = (Standby)list.get(0);
		return standby;
		
	}

}
